<?php
namespace App\Admin\Controllers\Stocking;

use App\Admin\Actions\Stocking\ImportSkuOther;
use App\Admin\Actions\Stocking\OtherBatchApproval;
use App\Admin\Actions\Stocking\OtherBatchReject;
use App\Admin\Actions\Stocking\OtherBatchApply;
use App\Models\Basics\SysArea;
use App\Models\Stocking\StockingSkuOther;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class SkuOtherController extends AdminController{

    protected $title = '其它备货';

    protected function grid()
    {
        $grid = new Grid(new StockingSkuOther());
        $grid->model()->where('status','<>',3);
        $grid->column('area.area','站点')->totalRow('汇总');
        $grid->column('sku.psku_code','SKU');
        $grid->column('sku.psku_cn','产品名称')->style('max-width:150px');
        $grid->column('apply_num','备货申请')->totalRow()->editable();
        $grid->column('remark','备货原因')->style('max-width:150px')->editable();
        $grid->column('approval_num','备货审批')->totalRow()->editable();
        $grid->column('status','状态')->display(function ($value){
            return StockingSkuOther::$_status[$value];
        });
        $grid->column('seller_id','备货员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('approval_date','审批日期');
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportSkuOther());
        });
        $grid->batchActions(function ($batch) {
            $batch->add(new OtherBatchApply());
            if(Admin::user()->can('stocking.other-approval')){
                $batch->add(new OtherBatchReject());
                $batch->add(new OtherBatchApproval());
            }
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, 'SKU')->placeholder('模糊查询SKU，名称');
                $filter->equal('area_id','站点')->select(SysArea::options());
            });
            $filter->column(1/2, function ($filter){
                $filter->equal('seller_id', '备货员')->select(User::getUserOptions());
                $filter->equal('status', '状态')->select(StockingSkuOther::$_status);
            });
        });

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new StockingSkuOther());
        $form->text('apply_num');
        $form->text('approval_num');
        $form->text('remark');
        return $form;
    }
}
